package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.domain.po.PointsRecord;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.SignInMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class SignPointsListener {
    private final IPointsRecordService pointsRecordService;

    //1.对于签到的积分服务
    @RabbitListener(bindings = @QueueBinding(value = @Queue("sign.points.queue"),
                    exchange = @Exchange(value =MqConstants.Exchange.LEARNING_EXCHANGE,type = ExchangeTypes.TOPIC),
                    key = MqConstants.Key.SIGN_IN))
    public void signIn(SignInMessage message) {
        log.info("收到签到消息,{}", message);
        //获悉签到积分和用户id,记录到数据库中
        pointsRecordService.addRecord(message.getUserId(),message.getPoints(),
                PointsRecordType.SIGN);
    }

    //2.对于学习的积分服务
    @RabbitListener(bindings = @QueueBinding(value = @Queue("learning.points.queue"),
                    exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE,type = ExchangeTypes.TOPIC),
                    key = MqConstants.Key.LEARN_SECTION))
    public void LearnedIn(SignInMessage message) {
        log.info("收到学习完毕消息,{}", message);
        //获悉签到积分和用户id,记录到数据库中
        pointsRecordService.addRecord(message.getUserId(),message.getPoints(),
                PointsRecordType.LEARNING);
    }

    //3.对于回答问题的积分服务
    @RabbitListener(bindings = @QueueBinding(value = @Queue("qa.points.queue"),
                    exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE,type = ExchangeTypes.TOPIC),
                    key = MqConstants.Key.WRITE_REPLY))
    public void ReplyIn(SignInMessage message) {
        log.info("收到学习完毕消息,{}", message);
        //获悉签到积分和用户id,记录到数据库中
        pointsRecordService.addRecord(message.getUserId(),message.getPoints(),
                PointsRecordType.QA);
    }
}
